import { useEffect, useState } from 'react';
import { useTranslation } from 'react-i18next';

export function useLanguage() {
  const { i18n } = useTranslation();
  const [isClient, setIsClient] = useState(false);

  useEffect(() => {
    setIsClient(true);
  }, []);

  const changeLanguage = (lang: 'en' | 'zh') => {
    if (isClient) {
      localStorage.setItem('dtv-language', lang);
      i18n.changeLanguage(lang);
    }
  };

  return {
    currentLanguage: isClient ? i18n.language : 'en',
    changeLanguage,
    isClient,
  };
}